require('./index.css');
var _account_service = require('service/account-service.js');
var $ = require('jquery');
const _catalog_service = require('../../../service/catalog-service');
var _util = require('util/util.js');

var header = {
    init : function(){
        $('#searchResult').hide();
        this.bindEvents();
        this.loadAccountInfo();
        return this;
    },
    bindEvents : function(){
        var _this = this;

        $("#signout-btn").on("click",function(){
            _account_service.logout(
                function(res){
                    window.location.replace('./catalog-main.html');
                },
                function(errMsg){
                    alert("退出失败");
                }
            );
        });
        
        $('#searchProducts').click(
            function(){
                console.log("search");
                _this.searchProducts();
            }
        )
    },
    
    loadAccountInfo : function(){
        var _this = this;
        _account_service.checkLogin(
            function(res){
                $('#signInSpan').hide();
                $('#signOutSpan').show();
            },
            function(errMsg){
                $('#signOutSpan').hide();
                $('#signInSpan').show();
            }
        );
    },

   
    searchProducts : function(){
        var keyword = $.trim($('#keyword').val());
        _catalog_service.getProductListByKeyword(
            keyword,
            function(res){
                sessionStorage.setItem("searchProduct",JSON.stringify(res));
                window.location.href = _util.getURLParam('redirect')||'./catalog-search.html';
            },
            function(errMsg){}
        )
    },

};

module.exports = header.init();